//
//  SEGActivityLotteryWinRecordCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/15.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityLotteryWinRecordCell.h"
#import "SEGActivityWinRecordView.h"

@interface SEGActivityLotteryWinRecordCell ()

@property (nonatomic, strong) UILabel* lotteryUserNum; //抽奖总人数
@property (nonatomic, strong) UIView* lineView;
@property (nonatomic, strong) UIImageView* noDateImage;
@property (nonatomic, strong) UILabel* noDateLabel;
@property (nonatomic, strong) SEGActivityWinRecordView *winRecordView;

@end

@implementation SEGActivityLotteryWinRecordCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;

    UILabel *label = [[UILabel alloc] init];
    label.font = kFont_14;
    label.textColor = kColor_T;
    label.text = @"最新动态";
    [self.contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(self.contentView);
        make.height.mas_equalTo(44.0);
    }];

    //分割竖线
    UIView *sepreateLine = [[UIView alloc] init];
    sepreateLine.backgroundColor = kColor_L2;
    [self addSubview:sepreateLine];
    [sepreateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(15);
        make.centerY.equalTo(label);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(0.5);
    }];
    
    self.lotteryUserNum = [[UILabel alloc] init];
    self.lotteryUserNum.font = kFont_13;
    self.lotteryUserNum.textColor = kColor_1;
    [self.contentView addSubview:self.lotteryUserNum];
    [self.lotteryUserNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sepreateLine.mas_right).offset(15);
        make.centerY.equalTo(label);
        make.height.mas_equalTo(44.0);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.top.equalTo(label.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    self.lineView = line;
    
    self.noDateImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_nowin_g")]];
    [self.contentView addSubview:self.noDateImage];
    [self.noDateImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom).offset(30);
        make.centerX.equalTo(self.contentView);
    }];
    
    self.noDateLabel = [[UILabel alloc] init];
    self.noDateLabel.font = kFont_13;
    self.noDateLabel.textColor = kColor_3;
    self.noDateLabel.text = @"还没有邻居中奖哦，赶紧去试试吧！";
    [self.contentView addSubview:self.noDateLabel];
    [self.noDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.noDateImage.mas_bottom).offset(9);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-29);
    }];
}

- (void)setUpRecordCellWithLotteryDetailModel:(SEGActivityLotteryDetailModel *)model
{
    if ([model.lotteryUserNum intValue] <= 0) {
        self.lotteryUserNum.text = @"还在等什么！赶紧点击抽奖呀！";
        self.noDateImage.hidden = YES;
        self.noDateLabel.hidden = YES;
        [self.winRecordView removeFromSuperview];
        [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.bottom.equalTo(self.lineView.mas_top);
        }];
    }else {
        self.lotteryUserNum.text = [NSString stringWithFormat:@"已有 %@ 名邻居参与了抽奖！",model.lotteryUserNum];
        if (model.lotteryUsers.count == 0) {
            self.noDateImage.hidden = NO;
            self.noDateLabel.hidden = NO;
            [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.equalTo(self);
                make.bottom.equalTo(self.noDateLabel.mas_bottom).offset(29);
            }];
            [self.winRecordView removeFromSuperview];
        }else {
            self.noDateImage.hidden = YES;
            self.noDateLabel.hidden = YES;
            NSUInteger row = MIN(model.lotteryUsers.count, 3);
            CGFloat winRecordViewHeight = kLotteryTextFont * row + kLotteryPadding * (row + 1);
            
            //  第一次直接创建轮播视图
            if (!self.winRecordView) {
                self.winRecordView = [[SEGActivityWinRecordView alloc] initWithFrame:CGRectMake(0, 44.0, LYScreenWidth(), winRecordViewHeight) type:SEGWinReocordViewType_Lottery dataSouce:model.lotteryUsers];
            } else {
                // 如果数据有刷新 就重新赋值轮播视图
                BOOL __block needResetWinRecordView = NO;
                NSArray<SEGActivityLotteryUsersModel *> *oldLotterys = self.winRecordView.lotteryModelArray;
                NSArray<SEGActivityLotteryUsersModel *> *newLotterys = model.lotteryUsers;
                if (oldLotterys.count != newLotterys.count) {
                    needResetWinRecordView = YES;
                } else {
                    [oldLotterys enumerateObjectsUsingBlock:^(SEGActivityLotteryUsersModel * _Nonnull oldObj, NSUInteger idx, BOOL * _Nonnull stop) {
                        SEGActivityLotteryUsersModel *newObj = newLotterys[idx];
                        // 有一项值不一致就重新加载
                        if (![oldObj.lotteryTime isEqualToString:newObj.lotteryTime] || ![oldObj.nickName isEqualToString:newObj.nickName] || ![oldObj.prizeName isEqualToString:newObj.prizeName]) {
                            needResetWinRecordView = YES;
                            *stop = YES;
                        }
                    }];
                }
                
                // 重新加载轮播视图
                if (needResetWinRecordView) {
                    [self.winRecordView removeFromSuperview];
                    self.winRecordView = nil;
                    self.winRecordView = [[SEGActivityWinRecordView alloc] initWithFrame:CGRectMake(0, 44.0, LYScreenWidth(), winRecordViewHeight) type:SEGWinReocordViewType_Lottery dataSouce:model.lotteryUsers];
                }
            }
            [self addSubview:self.winRecordView];
            
            [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.equalTo(self);
                make.bottom.equalTo(self.lineView.mas_bottom).offset(winRecordViewHeight);
            }];
        }
    }
}
@end
